package com.lfg.leetcode.leetcode0;

/**
 * 给定一个字符串 s，找到 s 中最长的回文子串
 */
public class No6ConvertZStr {

    public static void main(String[] args) {
        System.out.println(convert("AB", 1));
//        System.out.println(convert("0123456789012345678901234567890", 4));
//        System.out.println(convert("PAYPALISHIRING", 4));
    }


    private static String convert(String s, int numRows) {
        if (null == s || "".equals(s)) {
            return "";
        }
        if (numRows == 1) {
            return s;
        }
        char[] str = s.toCharArray();
        char[] ret = new char[str.length];
        int n = 0;
        int len = numRows - 1;
        for (int r = 0; r < numRows; r++) {
            for (int i = 0; n < str.length; i++) {
                int index = r + i * 2 * len;
                if (index >= str.length) {
                    break;
                }
                ret[n++] = str[index];
                int pos = (i + 1) * 2 * len - r;
                int rows = r % numRows;
                if (rows != 0 && rows != len && pos < str.length && n < str.length) {
                    ret[n++] = str[pos];
                }
            }
        }
        return new String(ret);
    }
}
